<?php

namespace App\Models;

class Test extends BaseModel
{
    public $orderBy = "id";
    protected $fillable = ['id','ids','name','test2Id','created_at_range'];
    protected $hidden = ['test2Id'];
    protected static $booted = [Test2::class];
    // 每次booting时都会自动运行的
    protected $dispatchesEvents = ['booting'=>Test3::class,'booted'=>Test3::class];
    protected static $traitInitializers = [];
    public function condition()
    {
        parent::condition(); // TODO: Change the autogenerated stub
        $this->sqlSearch('created_at','between',$this->getAttribute('created_at_range'));
    }

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        self::deleting(function ($model){
            $test2 = $model->test2;
            foreach ($test2 as $v){
                $v->delete();
            }
        });
    }
    // test2
    public function test2(){
        return $this->hasMany(Test2::class,'testId');
    }
}
